package com.cfushn.ddd.tools.maths;

import java.math.BigDecimal;

/**
 * 各类数值,String转化,以及常用计算
 *
 * @author CFuShn 2022/3/7 16:03
 */
@SuppressWarnings("unused")
public class MyNumberUtil {

    private static final double ZERO = 0.000001;

    /**
     * 同比计算(返回一个0~1间的小数,保留3位)
     */
    public static double csy(BigDecimal now, BigDecimal last) {
        try {
            return digitsDouble(
                (now.subtract(last)).divide(last, BigDecimal.ROUND_HALF_UP).doubleValue(), 3);
        } catch (Exception e) {
            return 0.0;
        }
    }

    /**
     * 同比计算(返回一个0~100的小数,保留1位小数,单位%)
     */
    public static double csyPerCent(BigDecimal now, BigDecimal last) {
        try {
            return digitsDouble((now.subtract(last)).multiply(new BigDecimal(100))
                .divide(last, BigDecimal.ROUND_HALF_UP).doubleValue(), 1);
        } catch (Exception e) {
            return 0.0;
        }
    }

    /**
     * 同比计算(返回一个0~1间的小数,保留3位)
     */
    public static double csy(double now, double last) {
        try {
            return digitsDouble((division(now - last, last)), 3);
        } catch (Exception e) {
            return 0.0;
        }
    }

    /**
     * 同比计算(返回一个0~100的小数,保留1位小数,单位%)
     */
    public static double csyPerCent(double now, double last) {
        try {
            return digitsDouble((division((now - last) * 100, last)), 1);
        } catch (Exception e) {
            return 0.0;
        }
    }

    public static double division(BigDecimal dividend, BigDecimal divisor) {
        return division(getDouble(dividend), getDouble(divisor));
    }

    public static double division(double dividend, double divisor) {
        return division(dividend, divisor, 1);
    }

    public static double division(double dividend, double divisor, int digits) {
        if (abs(divisor - 0) < ZERO || digits < 0) {
            return 0;
        }
        return digitsDouble(dividend / divisor, digits);
    }

    public static double getDouble(Object num) {
        if (num instanceof BigDecimal) {
            return ((BigDecimal) num).doubleValue();
        }
        if (num instanceof String) {
            return Double.parseDouble((String) num);
        }
        if (num instanceof Integer) {
            return ((Integer) num).doubleValue();
        }
        if (num instanceof Double) {
            return (Double) num;
        }
        return 0.0;
    }

    //// 注释 会被下一个方法泛型擦除
    // public static double digitsDouble(double d, int digits) {
    //     return Double.parseDouble(String.format("%." + digits + "f", d));
    // }

    public static double digitsDouble(Object d, int digits) {
        String temp = "%." + digits + "f";
        return Double.parseDouble(String.format(temp, getDouble(d)));
    }

    public static String digitsToStr(Object d, int digits) {
        String temp = "%." + digits + "f";
        return String.format(temp, getDouble(d));
    }

    public static double abs(double d) {
        return d > 0 ? d : d * -1;
    }
}
